# -*- coding: utf-8 -*-
'''
Created on 2013-5-31
处理封包
@author: 不懂d叛逆
'''
import ccjhObj, pyccjh, ccjhMan, ccjhSkill, ccjhMagic

class messageHandle():
	def __init__(self, mainwindow):
		if isinstance(mainwindow, pyccjh.pyccjh):
			self.__ccjh = mainwindow
		self.__functions = {10:self.func10,
			 11:self.func11,
			 12:self.func12,
			 13:self.func13,
			 14:self.func14,
			 15:self.func15,
			 16:self.func16,
			 17:self.func17,
			 18:self.func18,
			 19:self.func19,
			 20:self.func20,
			 21:self.func21,
			 22:self.func22,
			 23:self.func23,
			 24:self.func24,
			 25:self.func25,
			 26:self.func26,
			 27:self.func27,
			 28:self.func28,
			 29:self.func29,
			 30:self.func30,
			 31:self.func31,
			 32:self.func32,
			 33:self.func33,
			 34:self.func34,
			 35:self.func35,
			 36:self.func36,
			 37:self.func37}
		return
	def AddText(self, string):
		self.__ccjh.AddText(string)
		return

	def func10(self, obj):
		# &C=10&R=6764&I=长安秦楚古道&N=秦楚古道&F=0&D=襄阳西北&
		self.__ccjh.mans.ClearItem()
		self.__ccjh.Target = 0
		self.__ccjh.where = obj  # 记录地址
		self.__ccjh.inCombat = obj.get("F") == "1"  # 是否在战斗中
		if not self.__ccjh.inCombat:
			self.__ccjh.user.team = -1
		self.__ccjh.mans.LongID = int(obj.get("R"))
		s = "你进入了%s。" % (obj.get("N"))
		self.AddText(s)
		if self.__ccjh.user.LongID > 0:
			self.__ccjh.connect.Loc(self.__ccjh.user.LongID)
		return

	def func11(self, obj):
		# &C=11&E=734&T=11&U=1&tea=1&ap=8&sta=100&
		E = int(obj.get("E"))
		man = ccjhMan.ccjhMan(obj)
		if E == self.__ccjh.user.LongID:
			self.__ccjh.user.AddItem(man)
		else:
			tea = man.get("tea")
			if len(tea) == 0:
				if len(man.get("ap")) > 0:
					man.data["tea"] = "0"
					man.team = 0
			else:
				man.team = int(tea)
			self.__ccjh.mans.AddItem(man)
		return

	def func12(self, obj):
		environment = int(obj.get("E"));
		target = int(obj.get("T"));
		if self.__ccjh.Target == target:
			self.__ccjh.Target = 0
		if environment == self.__ccjh.user.LongID:
			self.__ccjh.user.RemoveItem(target)
		else:
			self.__ccjh.mans.RemoveItem(target)
		return

	def func13(self, obj):
		# &C=13&T=99611754&S=hammer&L=60&P=466&B=1&
		# &C=13&T=99611754&S=bianfu_dao&L=13&P=23&
		# &C=13&T=99611754&S=blade&L=60&P=466&M=bianfu_dao&E=1&
		skill = ccjhSkill.ccjhSkill(obj)
		if skill.get("T") == str(self.__ccjh.user.LongID):
			self.__ccjh.user.FlushSkill(skill)

		if skill.get("E") == "1":
			cmdString = ""
			for s in self.__ccjh.user.skills.values():
				if len(s.get("N")) == 0:
					skillID = s.get("S")
					skillName = ccjhSkill.FindSkillNameMap(skillID)
					if isinstance(skillName, ccjhSkill.ccjhSkill) > 0:
						s.setTo16(skillName)
					else:
						thisSkill = "0 downloadskill 0 %s\t" % (skillID,)
						if len(cmdString) + len(thisSkill) > 510:
							self.__ccjh.connect.SendCmd(cmdString)
							cmdString = thisSkill
						else:
							cmdString += thisSkill
			if len(cmdString) > 0:
				self.__ccjh.connect.SendCmd(cmdString)
		return

	def func14(self, obj):
		if obj.get("T") == str(self.__ccjh.user.LongID):
			self.__ccjh.user.FlushEquip(obj)
		return

	def func15(self, obj):
		self.__ccjh.user.setTo(obj)
		return

	def func16(self, obj):
		# &C=16&S=piaoyibu&N=飘逸步法&P=0&E=dodge,&
		skill = ccjhSkill.ccjhSkill(obj)
		self.__ccjh.user.FlushSkill(skill)
		ccjhSkill.skillNameMap[skill.get("S")] = skill
		return

	def func17(self, obj):
		man = ccjhMan.ccjhMan(obj)
		if self.__ccjh.user.Contains(man):
			self.__ccjh.user.AddItem(man)
		else:
			self.__ccjh.mans.AddItem(man)
		return

	def func18(self, obj):
		if len(obj.get("F")) == 0:
			self.__ccjh.user.LongID = int(obj.get("U"))
			s = "【系统】%s已经运行了%s秒。当前版本%s。" % (obj.get("M"), obj.get("R"), obj.get("V"))
			self.AddText(s)
			self.__ccjh.connect.Loc(self.__ccjh.user.LongID)
		else:
			self.AddText(obj.get("M"))
		return
	def func19(self, obj):
		self.AddText(obj.get("M"))
		return

	def func20(self, obj):
		# Wakeup
		return

	def func21(self, obj):
		# TODO 表情
		return

	def func22(self, obj):
		# TODO:战斗
		self.AddText(obj.get("M"))
		return

	def func23(self, obj):
		# &C=23&T=3&tea=-1&sta=9&hp=37&mp=0&eh=186&mm=0&att=5&def=6&
		# &C=23&T=3&tea=1&ap=6&sta=20&hp=78&mp=1918&eh=186&mm=1000&att=5&def=6&
		# &C=23&T=3&tea=-1&sta=40&hp=2792&mp=0&eh=6960&mm=5885&att=144925&def=867149&
		if int(obj.get("T")) == self.__ccjh.user.LongID:
			self.__ccjh.user.setTo(obj)
		else:
			man = ccjhMan.ccjhMan(obj)
			self.__ccjh.mans.AddItem(man)
		return

	def func24(self, obj):
		# &C=24&R=0&
		# &C=24&R=307287&D=野竹林羊肠小路3&N=野竹林羊肠小路3&A=&
		if obj.get("R") == "0":
			self.__ccjh.doors = []
		else:
			self.__ccjh.doors.append(obj)
		return

	def func25(self, obj):
		# 好友
		# TODOprint(obj.ToString())
		return

	def func26(self, obj):
		return

	def func27(self, obj):
		# &C=27&N=99611754&n=大米&m=你看起来约五十多岁。生得面如桔皮，头肿如猪，让人不想再看第二眼。你武功看起来已有小成。你看起来可能受了点轻伤。&
		# &C=27&N=99611754&s=结束观察&c=&
		name = obj.get("n")
		if len(name) > 0:
			self.AddText("%s:%s" % (name, obj.get("m")))
		else:
			s = obj.get("s")
			if len(s) > 0:
				self.AddText("%s:%s" % (s, obj.get("c")))
		return

	def func28(self, obj):
		# &C=28&S=force&M=recover&N=复元术&R=20&A=100&T=1&V=3&
		magic = ccjhMagic.ccjhMagic(obj)
		self.__ccjh.user.FlushMagics(magic)
		return

	def func29(self, obj):
		# 指令失败
		self.AddText(obj.get("M"))
		return

	def func30(self, obj):
		self.AddText(obj.get("M"))
		return

	def func31(self, obj):
		self.AddText("【私聊】%s(%s)告诉你：%s\n" % (obj.get("n"), obj.get("i"), obj.get("M")))
		return

	def func32(self, obj):
		if obj.get("T") == str(self.__ccjh.user.LongID):
			# TODO:人物进入场景
			pass
		return

	def func33(self, obj):
		# TODO /who用户列表
		return

	def func34(self, obj):
		# TODOprint(obj.ToString())
		return

	def func35(self, obj):
		return

	def func36(self, obj):
		return

	def func37(self, obj):
		# TODO 商人交易
		print(obj.ToString())
		return


	def notify(self, s):
		#print(s)
		obj = ccjhObj.ccjhObj(s)
		Type = obj.getType()
		if 10 <= Type <= 37:
			return self.__functions[Type](obj)
		else:
			return False
